package com.daoyintech.services;

import com.daoyintech.entity.bean.Event;
import com.daoyintech.entity.weixin.request.WeixinHongBaoEventRequest;
import com.daoyintech.entity.weixin.response.WeixinEventResponse;
import com.daoyintech.repository.EventRepository;
import com.daoyintech.repository.RedEnvelopeRepository;
import com.daoyintech.utils.WebUtils;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * Created by xuzhaolin on 15/12/29.
 */

@Service
public class EventService implements EnvironmentAware{


    private static final String success_flag = "SUCCESS";
    private static final String createEventUrl="https://api.weixin.qq.com/shakearound/lottery/addlotteryinfo?";
    private static final String toggleEventUrl="https://api.weixin.qq.com/shakearound/lottery/setlotteryswitch?access_token={access_token}&lottery_id={lottery_id}&onoff={on_off}";


    @Autowired
    private EventRepository eventRepository;

    @Autowired
    private WxMpService wxMpService;

    private RelaxedPropertyResolver propertyResolver;
    private String mch_id;
    private String wxappid;
    private String paykey;
    private String logoUrl;
    private String jumpUrl;


    @Override
    public void setEnvironment(Environment environment) {
        this.propertyResolver = new RelaxedPropertyResolver(environment,"weixin.");
        this.mch_id  = propertyResolver.getProperty("store_id");
        this.wxappid = propertyResolver.getProperty("appid");
        this.paykey  = propertyResolver.getProperty("pay_key");
        this.logoUrl = propertyResolver.getProperty("logo_url");
        this.jumpUrl = propertyResolver.getProperty("jumpUrl");

    }

    public List<Event> findALl() {
        return (List<Event>) eventRepository.findAll();
    }

    public Event create(){
        Event event = createEvent(createWeixinEvent());
        return event;
    }

    private WeixinEventResponse createWeixinEvent(){
        String access_token = null;
        try {
            access_token = wxMpService.getAccessToken();
        } catch (WxErrorException e) {
            e.printStackTrace();
            return null;
        }
        String url = createEventUrl+"access_token="+access_token+"&use_template=1"+"&logo_url="+this.logoUrl;
        WeixinHongBaoEventRequest eventRequest = new WeixinHongBaoEventRequest();
        eventRequest.setTitle("中信成都分行");
        eventRequest.setDesc("红包抽奖");
        eventRequest.setOnoff(1);
        eventRequest.setBegin_time(WebUtils.getCurrentSeconds());
        eventRequest.setExpire_time(WebUtils.getDateSeconds());
        eventRequest.setSponsor_appid(this.wxappid);
        eventRequest.setTotal(1000);
        eventRequest.setJump_url(this.jumpUrl);
        eventRequest.setKey(this.paykey);
        RestTemplate restTemplate = new RestTemplate();
        WeixinEventResponse result = restTemplate.postForObject(url,eventRequest,WeixinEventResponse.class);
        return result;
    }

    private Event createEvent(WeixinEventResponse response){
        if(response.getErrcode() == 0){
            Event event = new Event();
            event.setLottery_id(response.getLottery_id());
            eventRepository.save(event);
            return event;
        }
        return null;
    }
}
